using UnityEngine;

public class FlyToTask : BaseTask
{
	public static FlyToTask instance;

	private Camera maincam;

	public Transform DesObj;

	private Vector3 Des_pos;

	public float timeoflimit;

	private float timecal;

	private Vector3 UIscreenpos;

	private float dirflagoffset;

	private float scale_rate;

	private Vector3 curscreenpos;

	public override void MyUpdate()
	{
		if (timebegin)
		{
			stoptime += Time.deltaTime;
			if (Mathf.FloorToInt(stoptime) == 1)
			{
				timeone();
			}
			else if (Mathf.FloorToInt(stoptime) == 4)
			{
				timefour();
			}
			if (stoptime >= 4f)
			{
				stoptime = 0f;
				timebegin = false;
			}
		}
		else if (!FinishState)
		{
			base.MyUpdate();
			taskpointfun();
		}
	}

	public void taskpointfun()
	{
		minimapcontroller.instance.show_taskflag(Des_pos, isshow: true);
		showUIflag(Des_pos);
		if (Vector3.Distance(plane_smothfollow.instance.plane_body.position, Des_pos) < 100f)
		{
			minimapcontroller.instance.show_taskflag(Des_pos, isshow: false);
			FinishState = true;
			isSuccess = true;
			TasksController.instance.speed_rate_flag = true;
			UIController.instance.show_time(0f);
			return;
		}
		timeoflimit -= Time.deltaTime;
		if (timeoflimit <= 0f)
		{
			FinishState = true;
			timeoflimit = 0f;
			isSuccess = false;
			TasksController.instance.speed_rate_flag = false;
		}
		UIController.instance.show_time(timeoflimit);
	}

	public void showUIflag(Vector3 despos)
	{
		dirflagoffset = 32f * GlobalInf.ratioReltoUI;
		curscreenpos = maincam.WorldToScreenPoint(despos);
		if (curscreenpos.x > 0f && curscreenpos.y > 0f && curscreenpos.x < (float)Screen.width && curscreenpos.y < (float)Screen.height && curscreenpos.z > 0f)
		{
			scale_rate = (2000f - Vector3.Distance(despos, maincam.transform.position)) / 2000f + 0.6f;
			if (scale_rate < 0.6f)
			{
				scale_rate = 0.6f;
			}
			if (scale_rate > 1f)
			{
				scale_rate = 1f;
			}
			UIController.instance.Des_flagshow(curscreenpos, istrue: true, scale_rate);
			UIController.instance.Des_dirshow(Vector3.zero, istrue: false);
			return;
		}
		scale_rate = (2000f - Vector3.Distance(despos, maincam.transform.position)) / 2000f + 0.4f;
		if (scale_rate < 0.4f)
		{
			scale_rate = 0.4f;
		}
		if (scale_rate > 0.6f)
		{
			scale_rate = 0.6f;
		}
		dirflagoffset *= scale_rate;
		UIController.instance.Des_flagshow(Vector3.zero, istrue: false);
		if (curscreenpos.z > 0f)
		{
			UIscreenpos = curscreenpos;
			if (UIscreenpos.x < 0f)
			{
				UIscreenpos.x = dirflagoffset;
			}
			if (UIscreenpos.x > (float)Screen.width)
			{
				UIscreenpos.x = (float)Screen.width - dirflagoffset;
			}
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
			UIController.instance.Des_dirshow(UIscreenpos, istrue: true, scale_rate);
			return;
		}
		UIscreenpos = curscreenpos;
		UIscreenpos.x = (float)Screen.width - UIscreenpos.x;
		UIscreenpos.y = (float)Screen.height - UIscreenpos.y;
		Vector3 vector = maincam.transform.InverseTransformPoint(despos);
		if (vector.x > 0f)
		{
			UIscreenpos.x = (float)Screen.width - dirflagoffset;
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
		}
		else
		{
			UIscreenpos.x = dirflagoffset;
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
		}
		UIController.instance.Des_dirshow(UIscreenpos, istrue: true, scale_rate);
	}

	public override void MyStart()
	{
		base.MyStart();
		if (instance == null)
		{
			instance = this;
		}
		maincam = Camera.main;
		Des_pos = cur_step.despos;
		timeoflimit = cur_step.timelimit;
	}

	public override void MyEnd()
	{
		base.MyEnd();
		if (instance != null)
		{
			instance = null;
		}
		if (UnityVersionUtil.IsActive(DesObj.gameObject))
		{
			UnityVersionUtil.SetActiveRecursive(DesObj.gameObject, state: false);
		}
		UIController.instance.Des_dirshow(Vector3.zero, istrue: false);
		UIController.instance.Des_flagshow(Vector3.zero, istrue: false);
		minimapcontroller.instance.show_taskflag(Des_pos, isshow: false);
	}

	public override void timeone()
	{
		base.timeone();
		if (!one)
		{
			one = true;
			UIController.instance.show_hitinfo(cur_step.hitinfo);
			UIController.instance.hitinfo_appear();
		}
	}

	public override void timefour()
	{
		base.timefour();
		if (!four)
		{
			four = true;
			UIController.instance.hitinfo_disappear();
			DesObj.transform.position = Des_pos;
			UnityVersionUtil.SetActiveRecursive(DesObj.gameObject, state: true);
			creat_NPC();
		}
	}

	public void creat_NPC()
	{
		for (int i = 0; i < cur_step.enemy_id_num.Count; i++)
		{
			AIPool.instance.creat_AI(cur_step.enemy_id_num[i]);
		}
		for (int j = 0; j < cur_step.fri_id_num.Count; j++)
		{
			FriendPool.instance.creat_AI_Fri(cur_step.fri_id_num[j]);
		}
	}
}
